<?php
// $Id$

/**
 * @file
 * TODO
 */

/**
 * Implementation of hook_nodeapi().
 */
//function portals_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  //portals_include('hooks');
  //_portals_nodeapi($node, $op, $teaser, $page);
//}

/**
 * Implementation of hook_form_alter().
 */
//function portals_form_alter(&$form, $form_state, $form_id) {
  //portals_include('form');
  //_portals_form_alter($form, $form_state, $form_id);
//}


function _portals_form_alter(&$form, $form_state, $form_id) {
  portals_include('queries');
  // if we are on a node creation form
  if ($form['#id'] == 'node-form') {
    // the default is blank if its a new node
    $default_value = '';
    // and there is portal value then we are on the preview form
    if (isset($form_state['values']['portal'])) {
      // and so keep the portal values
      $default_value = $form_state['values']['portal'];
    }
    // if we have a node id then it is in edit mode
    elseif (isset($form['nid']['#value']) && ($form['nid']['#value'] != '')) {
      // and so go get the portal settings
      $default_value = _portals_node_get_nme_from_nid($form['nid']['#value']);
    }
    
    $form['portals'] = array(
      '#type' => 'fieldset',
      '#title' => t('Portals'),
      '#collapsible' => TRUE,
    );
    
    // and insert the portal form into the form
    $form['portals']['portal'] = array(
      '#type' => 'textfield',
      '#title' => 'Insert Portal',
      '#weight' => '0',
      '#description' => 'Choose a portal to insert into the current page.',
      // with the default value from above.
      '#default_value' => $default_value
    );
  }
}

 
 
function _portals_insertinto_node(&$node, $portal, $weight) {
  portals_include('content');
  if (is_string($portal)) {
    $portal = _portals_node_portal_portalid_from_name($portal);
  }
  // do error check here for if its -1
  $portal = portals_render($portal);
  
  if (!isset($node->content['portals'])) {
    $node->content['portals'] = array(
      '#value' => $portal,
      '#node' => $node,
      '#weight' => $weight,
    );
  }
}



/**
 * Implementation of hook_nodeapi().
 */
function _portals_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  // If we re viewing a page
  if ($op = 'view') {
    $pid_if_there_is_one = _portals_node_portal_pid_from_nid($node->nid);
    if ($pid_if_there_is_one != -1) {
      _portals_insertinto_node($node, $pid_if_there_is_one, 1);
    }
    // and it has an operation and a portal value
    if (isset($node->op) && isset($node->portal)) {
      // then if we are previewing it then
      if (($node->op == 'Preview') && ($node->portal != "")) {
        // insert the portal
        _portals_insertinto_node($node, $node->portal, 1);
      }
      // are we saving the page, if so if it has a node id
      if (($node->op == 'Save') && ($node->nid != '')) {
        // then save the portal id to the node id
        global $_portals_isset;
        if ($portals_isset != TRUE) {
          //dpr($node);
          _portals_node_set_pid_nid_from_nme($node->nid, $node->portal);
          $portals_isset = TRUE;
        }
      }
    }
  }
}


function _portals_node_set_pid_nid_from_nme($nid, $pname) {
  if ($pname == "") {
    if (_portals_node_portal_pid_from_nid($nid) != -1) {
      db_query(
       "DELETE FROM {portals_nodes}
        WHERE nid = %d"
        , $nid);
      drupal_set_message('Portal removed.');
    }
  }
  else {
    // Get a pid from the portal name
    $the_pid = _portals_node_portal_portalid_from_name($pname);
    if ($the_pid != -1) {
      // check if there is a node with the same id in the database
      $pid_if_there_is_one = _portals_node_portal_pid_from_nid($nid);
      // if there is
      if ($pid_if_there_is_one != -1) {
        // check if its upto date
        if ($pid_if_there_is_one == $the_pid) {
          // if its the same
          drupal_set_message('Portal unchanged.');
        }
        else {
          // and if it isnt hen update it
          db_query(
           "UPDATE {portals_nodes}
            SET pid = %d
            WHERE nid = %d"
            , $the_pid, $nid);
          drupal_set_message('Portal updated on page.');
        }
      }
      else {
        // if not add it
        db_query(
         "INSERT INTO {portals_nodes}
         (nid, pid)
          VALUES (%d,%d)"
          , $nid, $the_pid);
        drupal_set_message('Portal added to page.');
      }
    }
    else {
      // the pid does not exist, do not edit
      drupal_set_message('Portal with that name does not exist.');
    }
  }
}
